package com.ztgf.order.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.order.mapper.OrderSalesStatisticsMapper;
import com.ztgf.order.models.bo.OrderShopStatisticsBO;
import com.ztgf.order.models.bo.OrderStatisticsBO;
import com.ztgf.order.models.service.IOrderSalesStatisticsService;
import com.ztgf.order.models.vo.OrderSalesCategoryNewVO;
import com.ztgf.order.models.vo.OrderSalesCategoryShopVO;
import com.ztgf.order.models.vo.OrderSalesCategoryVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import static com.ztgf.common.utils.UtilConstants.TWO;
import static com.ztgf.common.utils.UtilConstants.isNull;

/**
 * author: chenyu.wang
 * date: 2020-07-22 15:48
 * Description: 订单销量统计服务服务
 */
@Slf4j
@DubboService
public class OrderSalesStatisticsServiceImpl implements IOrderSalesStatisticsService {

    @Autowired
    private OrderSalesStatisticsMapper orderSalesStatisticsMapper;

    @Override
    public String queryOrderSalesToDay(OrderStatisticsBO bo) {
        BigDecimal price = orderSalesStatisticsMapper.queryOrderSalesToDay(bo);
        String realPrice;
        realPrice = price.setScale(TWO, BigDecimal.ROUND_HALF_UP).toString();
        return realPrice;
    }

    @Override
    public String queryOrderDealCount(OrderStatisticsBO bo) {
        BigDecimal price = orderSalesStatisticsMapper.queryOrderDealCount(bo);
        String realPrice;
        realPrice = price.setScale(TWO, BigDecimal.ROUND_HALF_UP).toString();
        return realPrice;
    }

    @Override
    public Page<OrderSalesCategoryNewVO> queryOrderSalesCategory(OrderStatisticsBO bo) {
//        Page<OrderSalesCategoryVO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
//        List<OrderSalesCategoryVO> list = orderSalesStatisticsMapper.queryAllFianlCategorySales(page);
//        if (isNull(list)) {
//            return null;
//        }
//        for (OrderSalesCategoryVO vo : list) {
//            String realPrice;
//            BigDecimal price = orderSalesStatisticsMapper.queryOrderSalesCategory(vo.getId(), bo);
//            realPrice = price.setScale(TWO, BigDecimal.ROUND_HALF_UP).toString();
//            vo.setOrderPrice(realPrice);
//        }
        Page<OrderSalesCategoryNewVO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        List<OrderSalesCategoryNewVO> list = orderSalesStatisticsMapper.queryNewOrderSalesCount(page, bo);
        list.sort(new Comparator<OrderSalesCategoryNewVO>() {
            public int compare(OrderSalesCategoryNewVO o1, OrderSalesCategoryNewVO o2) {
                if (o1.getOrderPrice().compareTo(o2.getOrderPrice()) > 0) {
                    return -1;
                }
                if (o1.getOrderPrice().equals(o2.getOrderPrice())) {
                    return 0;
                }
                return 1;
            }
        });
        page.setRecords(list);
        return page;
    }

    @Override
    public Page<OrderSalesCategoryShopVO> queryOrderVariousShopItems(OrderShopStatisticsBO bo) {
        Page<OrderSalesCategoryShopVO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        List<OrderSalesCategoryShopVO> list = orderSalesStatisticsMapper.querySalesShopCategory1(bo);
        if (isNull(list)) {
            return null;
        }
        List<OrderSalesCategoryShopVO> voList = orderSalesStatisticsMapper.queryOrderMoney(bo);
        if (voList!=null){
            for (OrderSalesCategoryShopVO vo : list) {
                boolean flag=true;
                for (OrderSalesCategoryShopVO orderSalesCategoryShopVO : voList) {
                    if (vo.getShopId()!=null&&vo.getCategoryId()!=null){
                        if (vo.getShopId().equals(orderSalesCategoryShopVO.getShopId()) && vo.getCategoryId().equals(orderSalesCategoryShopVO.getCategoryId())){
                            vo.setOrderSales(orderSalesCategoryShopVO.getOrderSales());
                            flag=false;
                        }
                    }
                }
                if (flag){
                    vo.setOrderSales(new BigDecimal("0.00").toString());
                }
            }
            for (OrderSalesCategoryShopVO orderSalesCategoryShopVO : voList) {
                if (orderSalesCategoryShopVO.getCategoryId()==null){
                    orderSalesCategoryShopVO.setCategoryName("虚拟商品类目");
                    list.add(orderSalesCategoryShopVO);
                }
            }
        }else {
            for (OrderSalesCategoryShopVO orderSalesCategoryShopVO : list) {
                orderSalesCategoryShopVO.setOrderSales(new BigDecimal("0.00").toString());
            }
        }

     //   list= list.stream().sorted(Comparator.comparing(OrderSalesCategoryShopVO::getOrderSales).reversed().thenComparing(OrderSalesCategoryShopVO::getShopId)).collect(Collectors.toList());
       list.sort((OrderSalesCategoryShopVO a,OrderSalesCategoryShopVO b)->Double.valueOf(b.getOrderSales()).compareTo(Double.valueOf(a.getOrderSales())));

        int count=0;
        if(list != null && list.size() > 0) {
            page.setTotal(list.size());
            count = list.size();
            int fromIndex = (bo.getPageNo()-1) * bo.getPageSize();
            int toIndex = bo.getPageNo() * bo.getPageSize();
            if (toIndex > count) {
                toIndex = count;
            }
            List<OrderSalesCategoryShopVO> orderSalesCategoryShopVOS = list.subList(fromIndex, toIndex);
            page.setRecords(orderSalesCategoryShopVOS);

            return page;
        }
       page.setRecords(list);
        return page;
    }

    @Override
    public Page<OrderSalesCategoryNewVO> queryNewOrderSalesCount(OrderStatisticsBO bo) {
       // OrderShopStatisticsBO orderShopStatisticsBO = new OrderShopStatisticsBO();

       // List<OrderSalesCategoryShopVO> list = orderSalesStatisticsMapper.querySalesShopCategory1(orderShopStatisticsBO);
        Page<OrderSalesCategoryNewVO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        List<OrderSalesCategoryNewVO> list = orderSalesStatisticsMapper.queryNewOrderSalesCount(page, bo);
        page.setRecords(list);
        return page;
    }


}
